package com.gitee.feizns.webtemplate.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.gitee.feizns.webtemplate.Application;
import com.gitee.feizns.webtemplate.dao.mapper.BasesMapper;
import com.gitee.feizns.webtemplate.entity.ao.commons.page.PageFilter;
import com.gitee.feizns.webtemplate.entity.vo.Bases;
import lombok.Data;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 测试控制器
 * @author feizns
 * @since 2019/5/11 0011
 */
@RestController
public class TestController {

    @Autowired
    private Application.Test test;

    @Autowired
    private ApplicationEventPublisher publisher;

    /**
     * 测试方法
     * @param hello
     * @return
     * @throws InvalidFormatException
     */
    @GetMapping("/test")
    public Object test(Temp hello) throws InvalidFormatException {
        publisher.publishEvent("aabcc");
//        return hello;
        return "ab";
    }

    @Data
    public static class Temp {

        private Integer hello;

        public void setHello(Integer hello) {
            this.hello = hello;
        }

    }

    @Autowired
    private BasesMapper basesMapper;

    @GetMapping("/search")
    public Object search(PageFilter pageFilter) {
//        Page page = new Page();
//
////        page.setAsc(pageFilter.getOrder() == Order.asc ? true : false);
////
////        page.setOrderByField(pageFilter.getSort_by());
////
//
//        page.setDescs(Arrays.asList("id"));
//        page.setAscs(Arrays.asList("created_at"));
//
////        page.set
//
//        if ( pageFilter.getPage_no() != null )
//            page.setCurrent(pageFilter.getPage_no());
//
//        if ( pageFilter.getPer_page() != null )
//            page.setSize(pageFilter.getPer_page());
//
//        OrderColumn orderColumn = new OrderColumn("created_at", Order.desc);
//        OrderColumn orderColumn2 = new OrderColumn("id", Order.desc);
//        OrderColumn orderColumn3 = new OrderColumn("name", Order.desc);
//
////        orderColumn.setUsed(true);
////        System.out.println(orderColumn.isUsed());
//
//        OrderPagination bounds = new OrderPagination(2, 12, orderColumn, orderColumn2, orderColumn3);

        RowBounds bounds = pageFilter.toRowBounds();


//        OrderPagination pagination = new OrderPagination(1, 10);
//
//        List<Bases> all = basesMapper.getAll(pagination);
//
//        FvPageImpl fvPage = new FvPageImpl<>(pagination, all);

        List<Bases> all = basesMapper.getAll(new Page<>(1, 20));

        return all;
    }

    @Data
    public static class TT {

        private Integer id;

        @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
        private Date date;

        private List<String> list;

    }

    public static TT create() {
        TT tt = new TT();
        tt.setDate(new Date());
        tt.setId(null);
        return tt;
    }

}
